﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ProgNetComponentsDx.Browsers;

namespace Wydatki.Tools
{
    public partial class FormNoweProdukty : XtraForm
    {
        private FormNoweProdukty()
        {
            InitializeComponent();
        }
        
        Browser brKategorie;

        public FormNoweProdukty(Setup.DataSets.ProduktyDataSet ds)
        {
            InitializeComponent();
            brKategorie = browserProvider1.GetBrowser(Init.InitFormBrowserNames.Glowna.KategorieProduktow);
            produktyDataSet1 = ds;
            gridControl1.DataSource = ds;
            rep_STP_PROD_ID.ResultTable = ds.STP_PROD_KAT;

            gridControl2.DataSource = brKategorie.GetTable();
            if (!brKategorie.GetTable().Columns[brKategorie.ValueMember].AutoIncrement)
            {
                brKategorie.GetTable().Columns[brKategorie.ValueMember].AutoIncrement = true;
                brKategorie.GetTable().Columns[brKategorie.ValueMember].AutoIncrementSeed = -1;
                brKategorie.GetTable().Columns[brKategorie.ValueMember].AutoIncrementStep = -1;
            }
            gridView2.ActiveFilterString = "STP_KAT_PROD_ID < 0";
        }

        private void gridView2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                DataRow r = gridView2.GetFocusedDataRow();
                if (r != null)
                {
                    int id = r["STP_KAT_PROD_ID"].AsInt();
                    if (produktyDataSet1.STP_PROD_KAT.RowExists("STP_PRODK_KATEGORIA = " + id))
                    {
                        this.ShowDxInfoDialog("Kategoria jest użyta");
                    }
                    else
                    {
                        r.Delete();
                    }
                }
            }
        }

        bool ValidateAll()
        {
            return brKategorie.GetTable().ValidateColUnique("STP_KAT_PROD_NAZWA", "Nazwa powinna być unikalna");
        }
        
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (ValidateAll())
            {
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        private void gridView2_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {

        }
           
    }

}
